from sls_gc_MissionsLand_ui import Ui_MissionsLand
from PyQt6.QtWidgets import QWidget,QApplication
from PyQt6.QtGui import QBrush,QColor
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QDialog
from sls_gc_agent import sls_Agent
import sys

class gc_MisssionsLand(QDialog):
    def __init__(self,parent=None,fly:str=None,index:int=0):
        super().__init__(parent)
        self.add_index = index # 新增的任务索引，默认为0，即添加到列表的最前面，也可以根据需要修改为其他值，如self.ui.listWidget.currentRow()，表示添加到当前选中的任务后面，或者其他自定义的索引值。       
        self.ui = Ui_MissionsLand()
        self.ui.setupUi(self)
        self.parent = parent
        self.fly = fly
        self.setWindowTitle(f"{fly} 原地降落")
        self.ui.okpushButton.clicked.connect(self.pushButton_clicked)
        self.ui.checkBox.stateChanged.connect(self.checkBox_stateChanged)

    def checkBox_stateChanged(self, state):
        if state == 2:  # 选中状态
            self.ui.yawSpinBox.setEnabled(False)  # 禁用 yawSpinBox
        else:  # 未选中状态
            self.ui.yawSpinBox.setEnabled(True)  # 启用 yawSpinBox

    def pushButton_clicked(self):
        agent:sls_Agent = self.parent.parent.agent
        detection_time = self.ui.altitudeSpinBox.value()
        if self.ui.checkBox.isEnabled:
            yaw = None
        else:
            yaw = self.ui.yawSpinBox.value()
        #组合并发送
        landDict = {
            "missionsType": "MissionsLand",
            "datasets":{
                "landing_detection_time": detection_time,
                "yaw": yaw
            }
        }
            #agent.publish_MS_appendMission(self.fly,takeoffDict)
        agent.publish_MS_addMission(self.fly,landDict,self.add_index)
        
        self.parent.ask_missionslist()
        self.close()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = gc_MisssionsTakeoff()
    main_window.show()
    sys.exit(app.exec())